package com.awesome.sqlitedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.awesome.common.base.mvp.BaseActivity;
import com.awesome.common.constant.Constant;
import com.awesome.common.util.ToastUtils;
import com.awesome.common.util.log.ShowLogUtil;

public class MainActivity extends BaseActivity {

    private EditText mEtName;
    private EditText mEtAge;
    private EditText mEtId;
    private Button mBtnAdd;
    private Button mBtnUpdate;
    private Button mBtnDelete;
    private Button mBtnCreateTable;
    private Button mBtnDropTable;
    private Button mBtnSelect;
    private Button mBtnUpgradeDb;
    private SQLiteDatabase mDb;
    private String mNameStr;
    private String mAgeStr;
    private String mIdStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        DatabaseHelper databaseHelper = new DatabaseHelper(this, "school.db", null, 1);
        mDb = databaseHelper.getReadableDatabase();
        initListener();
    }


    private void initView() {
        mEtName = findViewById(R.id.et_name);
        mEtAge = findViewById(R.id.et_age);
        mEtId = findViewById(R.id.et_id);
        mBtnAdd = findViewById(R.id.btn_add);
        mBtnUpdate = findViewById(R.id.btn_update);
        mBtnDelete = findViewById(R.id.btn_delete);
        mBtnSelect = findViewById(R.id.btn_select);
        mBtnUpgradeDb = findViewById(R.id.btn_upgrade_db);
        mBtnCreateTable = findViewById(R.id.btn_create_table);
        mBtnDropTable = findViewById(R.id.btn_drop_table);
        if (!hasPermission(Constant.WRITE_EXTERNAL_PERMISSION)) {
            requestPermission(Constant.WRITE_EXTERNAL_CODE, Constant.WRITE_EXTERNAL_PERMISSION);
        }
    }

    private void initListener() {
        mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
                String insertSql = "insert into student (name,age) values (?,?)";
                try {
                    mDb.execSQL(insertSql, new String[]{mNameStr, mAgeStr});
                    ToastUtils.showToast(MainActivity.this, "插入成功");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        mBtnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
                String updateSql = "update student set name=? , age = ? where id=?";
                try {
                    mDb.execSQL(updateSql, new String[]{mNameStr, mAgeStr, mIdStr});
                    ToastUtils.showToast(MainActivity.this, "修改成功");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        mBtnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
                String selectSql = "delete from student";
                if (!TextUtils.equals(mIdStr, "")) {
                    selectSql += " where id=" + mIdStr;
                }
                try {
                    mDb.execSQL(selectSql);
                    ToastUtils.showToast(MainActivity.this, "删除成功");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        mBtnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
                String selectSql = "select * from student";
                if (!TextUtils.equals(mIdStr, "")) {
                    selectSql += " where id=" + mIdStr;
                }
                try {
                    Cursor cursor = mDb.rawQuery(selectSql, null);
                    while (cursor.moveToNext()) {
                        int studentId = cursor.getInt(0);
                        String studentName = cursor.getString(1);
                        int studentAge = cursor.getInt(2);
                        ShowLogUtil.info("studentId=" + studentId + ",studentName=" + studentName + ",studentAge=" + studentAge);
                        ToastUtils.showToast(MainActivity.this, "查询成功" + "studentId=" + studentId + ",studentName=" + studentName + ",studentAge=" + studentAge);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        mBtnCreateTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql = "create table if not exists student (id integer primary key autoincrement," +
                        "name varhcar(20)," +
                        "age integer)";
                mDb.execSQL(sql);
                ToastUtils.showToast(MainActivity.this, "创建表成功");
            }
        });
        mBtnDropTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql = "drop table if exists student";
                mDb.execSQL(sql);
                ToastUtils.showToast(MainActivity.this, "删除表成功");
            }
        });
        mBtnUpgradeDb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatabaseHelper databaseHelper = new DatabaseHelper(MainActivity.this, "school.db", null, 2);
                mDb = databaseHelper.getReadableDatabase();
                ToastUtils.showToast(MainActivity.this, "升级数据库成功");
            }
        });
    }

    private void getData() {
        mNameStr = mEtName.getText().toString();
        mAgeStr = mEtAge.getText().toString();
        mIdStr = mEtId.getText().toString();
    }
}